package cn.ztydata.teachingsystem.heaven.dao.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 教师收藏资源
 *
 * @author fanruiping
 * @since 2015-02-04
 */
@Repository
public class TeacherRscDao extends BaseRedisDao {
    protected static Logger logger = LoggerFactory.getLogger(BaseRedisDao.class); //日志记录器

    /**
     * 获取所有资源id
     * @param teacherId 教师编号
     * @return List
     *
     * @author fanruiping
     * @since 2015/03/18
     */
    public List<Long> getAll(final long teacherId, final long groupId, final int groupType) {
        ListOperations<String, Long> listOperations = redisTemplate.opsForList();

        logger.debug("获取所有收藏资源id集合");
        return listOperations.range(KeyUtils.teacherRsc(teacherId, groupType, groupId), 0, -1);
    }

    /**
     * 获取所有资源id
     * @param teacherId 教师ID
     * @param groupType 群组类型
     * @param groupIds 群组编号
     * @return List
     *
     * @author fanruiping
     * @since 2015-05-07
     */
    public List<Long> getAll(final long teacherId, final int groupType,final List<Long> groupIds) {
        ListOperations<String, Long> listOperations = redisTemplate.opsForList();

        List<Long> resourceIds = new ArrayList<>();

        logger.debug("循环获取资源编号:Long groupId : groupIds");
        //循环获取资源编号
        for (Long groupId : groupIds){
            resourceIds.addAll(listOperations.range(KeyUtils.teacherRsc(teacherId,groupType,groupId),0,-1));
        }

        return resourceIds;
    }

    /**
     * 保存教师收藏的资源
     * @param teacherId 教师编号
     * @param resourceId 资源编号
     * @return Boolean
     *
     * @author fanruiping
     * @since 2015-02-06
     */
    public Boolean addResource(final long teacherId, final long resourceId, final long groupId, final int groupType) {
        SessionCallback<Boolean> sessionCallback = new SessionCallback<Boolean>() {
            @Override
            public Boolean execute(RedisOperations operations) throws DataAccessException {
                operations.multi();

                //获取Set操作
                SetOperations<String, Long> setOperations = operations.opsForSet();

                //获取List操作
                ListOperations<String,Long> listOperations = operations.opsForList();

                //保存教师与资源的关系
                listOperations.rightPush(KeyUtils.teacherRsc(teacherId, groupType, groupId), resourceId);

                //保存知识点与资源的关系
                setOperations.add(KeyUtils.rscTeacher(resourceId, groupType, groupId), teacherId);

                operations.exec();
                return true;
            }
        };

        logger.debug("return 新增教师收藏资源执行结果");

        return (Boolean)redisTemplate.execute(sessionCallback);
    }

    /**
     * 删除教师收藏资源(一个)
     * @param teacherId 教师编号
     * @param resourceId 资源编号
     * @return Boolean
     *
     * @author fanruiping
     * @since 2015-02-06
     */
    public Boolean deleteResource(final long teacherId, final long resourceId, final long groupId, final int groupType) {
        SessionCallback<Boolean> sessionCallback = new SessionCallback<Boolean>() {
            @Override
            public Boolean execute(RedisOperations operations) throws DataAccessException {
                operations.multi();

                //获取Set操作
                SetOperations<String, Long> setOperations = operations.opsForSet();

                //获取List操作
                ListOperations<String, Long> listOperations = operations.opsForList();

                //删除教师->资源关系
                listOperations.remove(KeyUtils.teacherRsc(teacherId, groupType, groupId), 0, resourceId);

                //删除资源->教师关系
                setOperations.remove(KeyUtils.rscTeacher(resourceId, groupType, groupId), teacherId);

                operations.exec();
                return true;
            }
        };

        logger.debug("execute delete teacher resource");

        return (Boolean)redisTemplate.execute(sessionCallback);
    }

    /**
     * 教师是否已经收藏指定资源
     * @param teacherId 教师编号
     * @param resourceId 资源编号
     * @return Boolean
     *
     * @author fanruiping
     * @since 2015-02-10
     */
    public Boolean isCollected(final long teacherId, final long resourceId, final long groupId, final int groupType){
        SetOperations<String,Long> setOperations = redisTemplate.opsForSet();

        logger.debug("教师是否已经收藏指定资源");
        return setOperations.isMember(KeyUtils.rscTeacher(resourceId, groupType, groupId), teacherId);
    }
}
